
#include "Entities/BaseEntity.h"
#include "Entities/ComponentTypeEnum.h"
#include "Base.h"
#include "Game/Game.h"
#include "Sprite/SpriteRenderer.h"
#include "Sprite/SpriteTypes.h"
#include "Assets/AssetManager.h"
#include "Maths/Matrix3.h"
#include "Maths/Matrix4.h"
#include "Model3DComponent.h"
#include "Resources/ResourceManager.h"
#include "Graphics/Color.h"
#include "EventHandler/EventHandler.h"
#include "StringUtils.h"


#include <string>

#include <Memory/DebugNew.h>
#include "TransformComponent.h"
#include "Game/ModelRendererGameComponent.h"
#include "Game/GameInfo.h"

namespace CasaEngine
{

/**
 * 
 */
Model3DComponent::Model3DComponent(BaseEntity* pEntity_)
	: Component(pEntity_, MODEL3D),
	m_pModel(NULL),
	m_pTransform(NULL),
	m_pModelRenderer(NULL)
{
	
}

/**
 * 
 */
Model3DComponent::~Model3DComponent()
{

}


/**
 * 
 */
void Model3DComponent::Initialize()
{
	m_pTransform = GetEntity()->GetComponentMgr()->GetComponent<TransformComponent>();
	CASAENGINE_ASSERT(m_pTransform != NULL && "Model3DComponent::Initialize() can't find the TransformComponent. Please add it before add a Model3DComponent");

	m_pModelRenderer = Game::Instance()->GetGameComponent<ModelRendererGameComponent>();
	CASAENGINE_ASSERT(m_pModelRenderer != NULL && "Model3DComponent::Initialize() can't find the ModelRendererGameComponent.");
}

/**
 * 
 */
Model *Model3DComponent::GetModel() const
{
	return m_pModel;
}

/**
 * 
 */
void Model3DComponent::SetModel(Model *val)
{
	m_pModel = val;
}

/**
 * 
 */
IEffect *Model3DComponent::GetEffect() const
{
	return m_pEffect;
}

/**
 * 
 */
void Model3DComponent::SetEffect(IEffect *val)
{
	m_pEffect = val;
}

/**
 * 
 */
void Model3DComponent::Update(const GameTime& /*gameTime_*/)
{
	CASAENGINE_ASSERT(m_pModel != NULL);
	CASAENGINE_ASSERT(m_pModelRenderer != NULL);

	Matrix4 mat = m_pTransform->GetWorldMatrix();
	m_pModelRenderer->AddModel(m_pModel, mat, m_pEffect);
}

/**
 * 
 */
/*void Model3DComponent::HandleEvent(const Event* pEvent_)
{

}*/

/**
 * 
 */
void Model3DComponent::Write(std::ostream& /*os*/) const
{

}

/**
 * 
 */
void Model3DComponent::Read (std::ifstream& /*is*/)
{

}

} // namespace CasaEngine
